//
//  XYDownloadFootView.swift
//  XYReadBook
//
//  Created by fox on 2017/10/28.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import UIKit

class XYDownloadFootView: UIView {
    var clickDownLoad:((_ chapterCount:Int,_ downloadBtn:UIButton) ->Void)?
    var chapterCountLab = UILabel()// 章节count
    var lineView = UIView()
    var downloadBtn = UIButton() //下载按钮
    var chapterCount:Int = 0{
        didSet { // MARK - 这里的确是low,我尝试过改变button的normal 以及选中的切换,可不知道为啥,点击的时候,高亮的时候就会变化
            if chapterCount > 0 {
                downloadBtn.isEnabled = true
                downloadBtn.isSelected = true
                downloadBtn.setTitle("立即下载", for: .normal)
                downloadBtn.setTitleColor(UIColor.orange, for: .normal)
                downloadBtn.layer.borderColor = UIColor.orange.cgColor

                 chapterCountLab.isHidden = false
                let countStr = String.init(format: "已选择%zd章节", chapterCount)
                chapterCountLab.attributedText = changeTextChange(regex: "\\d+", text: countStr, color: UIColor.orange)
            }else {
                downloadBtn.setTitle("请选择下载章节", for: .normal)
                downloadBtn.setTitleColor(UIColor.init(red: 163/255.0, green: 163/255.0, blue: 163/255.0, alpha: 1), for: .normal)
                downloadBtn.layer.borderColor = UIColor.init(red: 163/255.0, green: 163/255.0, blue: 163/255.0, alpha: 1).cgColor
                downloadBtn.layer.borderColor =
                     UIColor.init(red: 163/255.0, green: 163/255.0, blue: 163/255.0, alpha: 1).cgColor
                downloadBtn.isEnabled = false
                chapterCountLab.isHidden = true
            }
            self.layoutIfNeeded()
        }
    }
    override init(frame: CGRect) {
       super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        let width = 80 * (ScreenWidth / 375.0)
        let btnW = ScreenWidth - (width * 2)

        downloadBtn.frame = CGRect.init(x: width, y: self.height - 46, width: btnW, height: 34)
        chapterCountLab.frame = CGRect.init(x: 0, y: 16, width: ScreenWidth, height: 20)
        lineView.frame = CGRect.init(x: 0, y: 1, width: self.width, height: 1)
    }

    func setupUI() {
        addSubview(chapterCountLab)
        addSubview(downloadBtn)
        if chapterCount == 0 {
            chapterCountLab.isHidden = true
        }else {
             chapterCountLab.isHidden = false
        }
        addSubview(lineView)
        lineView.backgroundColor = UIColor.init(red: 230/255.0, green: 230/255.0, blue: 230/255.0, alpha: 0.8)
        chapterCountLab.textAlignment = .center
        chapterCountLab.textColor = UIColor.init(red: 163/255.0, green: 163/255.0, blue: 163/255.0, alpha: 1)


        downloadBtn.setTitle("请选择下载章节", for: .normal)
        downloadBtn.setTitleColor(UIColor.init(red: 163/255.0, green: 163/255.0, blue: 163/255.0, alpha: 1), for: .normal)
        downloadBtn.layer.borderColor = UIColor.init(red: 163/255.0, green: 163/255.0, blue: 163/255.0, alpha: 1).cgColor
        downloadBtn.layer.borderWidth = 1
        downloadBtn.layer.cornerRadius = 17
        downloadBtn.addTarget(self, action: #selector(download), for: .touchUpInside)
        downloadBtn.isEnabled = false




    }

    func download() {
        clickDownLoad?(chapterCount,downloadBtn)
    }


    /// 将数字变幻颜色
    ///
    /// - Parameters:
    ///   - regex: 正则
    ///   - text: 全部文字
    ///   - color: 需要改变颜色
    /// - Returns: 返回已经变幻颜色的文字
    func changeTextChange(regex: String, text: String, color: UIColor) -> NSMutableAttributedString
    {
        let attributeString = NSMutableAttributedString(string: text)
        do {
            let regexExpression = try NSRegularExpression(pattern: regex, options: NSRegularExpression.Options())
            let result = regexExpression.matches(in: text, options: NSRegularExpression.MatchingOptions(), range: NSMakeRange(0, text.characters.count))
            for item in result {
                attributeString.addAttribute(NSForegroundColorAttributeName, value: color, range: item.range)
            }
        } catch {
            print("Failed with error: \(error)")
        }
        return attributeString
    }
}
